@extends('admin.index.layout')

@section('title', $title)

@section('content')
    
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                网站配置
                <small>添加修改配置</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> 主页</a></li>
                <li><a href="#">网站配置</a></li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <!-- left column -->
                <div class="col-md-12">
                    <!-- general form elements -->
                    <div class="box box-primary">
                        <div class="box-header with-border">
                            <h3 class="box-title">快速添加</h3>
                        </div><!-- /.box-header -->
                        <!-- form start -->
                        <form role="form" action="{{url('/admin/config/add')}}" method="post" enctype="multipart/form-data">

                            <div class="box-body">
                                @if (count($errors) > 0)
                                    <div id="jg" class="alert alert-danger">
                                        <ul>
                                            @foreach ($errors->all() as $error)
                                                <li>{{ $error }}</li>
                                            @endforeach
                                        </ul>
                                    </div>
                                @endif
                            {{ csrf_field() }}
                                <div class="form-group">
                                    <label for="exampleInputName">网站标题</label>
                                    <input name="title" type="text" class="form-control" id="exampleInputName"
                                           placeholder="{{$data->title or '网站标题'}}">
                                </div>
                                <div class="form-group">
                                    <label for="exampleInputName">关键字</label>
                                    <input name="keywords" type="text" class="form-control" id="exampleInputName"
                                           placeholder="{{$data->keywords or '关键词'}}">
                                </div>
                                <div class="form-group">
                                    <label for="exampleInputName">网站描述</label>
                                    <input name="discription" type="text" class="form-control" id="exampleInputName"
                                           placeholder="{{$data->discription or '网站描述'}}">
                                </div> 
                                
                                <div class="form-group">
                                    <label for="exampleInputName">版权</label>
                                    <input name="copyright" type="text" class="form-control" id="exampleInputName"
                                           placeholder="{{$data->copyright or '版权'}}">
                                </div>
                                <div class="form-group">
                                    <label for="exampleInputName">网站状态</label><br>
                                    <div class="col-md-1">
                                        @if(!$data or $data->open ==1)
                                                <button class="btn btn-block btn-success btn-flat" id="open" onclick="return false">开启</button>
                                        @else
                                            <button class="btn btn-block btn-danger btn-flat" id="open" onclick="return false">关闭</button>
                                        @endif
                                        
                                    </div><br><br>
                                </div>
                                <div class="form-group">
                                    <label for="exampleInputFile">原logo</label><br>
                                    <img height="100px" src="{{ url("uploads")}}/{{$data->logo or 'logo.jpg'}}">
                                </div>
                                
                                <div class="form-group">
                                    <label for="exampleInputFile">logo</label>
                                    <input type="file" id="exampleInputFile" name="logo">
                                    <p class="help-block">请上传网站logo</p>
                                    <p id="demo_result"></p>
                                </div>
                            </div><!-- /.box-body -->

                            <div class="box-footer">
                                <button type="submit" class="btn btn-primary">提交</button>
                                <button type="reset" class="btn btn-default">重置</button>
                            </div>
                        </form>
                    </div><!-- /.box -->

                </div><!--/.col (left) -->
                <!-- right column -->
        </div>
    </div>

     <script type="text/javascript">
        window.onload = function()
        {
            //预览
            var result = document.getElementById("demo_result");
            var input = document.getElementById("exampleInputFile");

            if(typeof FileReader === 'undefined'){
                result.innerHTML = "<p class='warn'>抱歉，你的浏览器不支持 FileReader</p>";
                input.setAttribute('disabled','disabled');
            }else{
                input.addEventListener('change',readFile,false);
            }
            
            function readFile(){
                var file = this.files[0];
                if(!/image\/\w+/.test(file.type)){
                    alert("请确保文件为图像类型");
                    return false;
                }
                var reader = new FileReader();
                reader.readAsDataURL(file);
                reader.onload = function(e){
                    result.innerHTML = '<img height="100px" src="'+this.result+'" alt=""/>'
                }
            }

            // alert($);
            setInterval(function(){
                $('#jg').fadeOut();
                
            }, 2000);
            // 开关按钮
            // $('#open').on('click',function(){
                // return false;});
            $('#open').on('click',function(){
                // return false;
                $.ajax({
                        async:true,
                        url:"{{ asset('admin/config/ajax') }}",
                        type:'GET',
                        success:function(data)
                        {
                            if(data == 1){
                                $('#open').attr('class','btn btn-block btn-success btn-flat').html('开启');
                            }else{
                                $('#open').attr('class','btn btn-block btn-danger btn-flat').html('关闭');
                            }
                        },
                        error:function()
                        {
                            $('#open').attr('class','btn btn-block btn-danger btn-flat').html('请重试');
                        },
                        timeout:1000

                    });
            });

        }

     </script>
              
@endsection